//
//  TextMessageTableViewCell.m
//  TQY
//
//  Created by 甘世清 on 2024/10/28.
//

#import "TextMessageTableViewCell.h"
@interface TextMessageTableViewCell ()

@property (nonatomic, strong) UIImage *bubble;

@property (nonatomic, strong) UIImageView *bubbleImageView;

@property (nonatomic, strong) YYLabel * bubbleText;

@end
@implementation TextMessageTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    [self.contentView addSubview:self.bubbleImageView];
    [self.bubbleImageView addSubview:self.bubbleText];
    [self.contentView addSubview:self.hudView];
    [self.hudView addSubview:self.hud];
    [self.hudView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(20);
        make.right.equalTo(self.bubbleImageView.mas_left).offset(-2);
        make.bottom.equalTo(self.bubbleImageView.mas_bottom);
    }];
    [self.contentView addSubview:self.reloadBtn];
    [self.reloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(40);
        make.right.equalTo(self.bubbleImageView.mas_left);
        make.centerY.equalTo(self.bubbleImageView);
    }];
    self.FS_cellBottomView = self.bubbleImageView;
}
- (void)setModel:(IMMessageModel *)model{
    [super setModel:model];
   
    CGSize rectsize = model.textLayout.textBoundingSize;
    self.bubbleText.frame = CGRectMake(15, 0.0f, rectsize.width, MAX(rectsize.height, 24));
    self.bubbleText.textLayout = self.model.textLayout;
    float width = self.bubbleText.frame.size.width+32;
//    if (width<50) {
//        width =50;
//        self.bubbleText.textAlignment = YYTextVerticalAlignmentCenter;
//    }
    if (width>SCREEN_WIDETH-120) {
        width = SCREEN_WIDETH-120;
    }
    self.bubbleImageView.frame = CGRectMake(model.isSend?SCREEN_WIDETH-width-70:70, self.headerView.y+5, width, self.bubbleText.frame.size.height+18);
    self.bubbleText.frame = CGRectMake(model.isSend?12:18, 10, width-24, self.bubbleText.frame.size.height);
    
    UIImage *bubble = [[UIImage alloc] init];
    bubble = [UIImage imageNamed:model.isSend?@"chat_bg_others_self":@"chat_bg_other"];
    [self.bubbleImageView setImage:[bubble resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 28) resizingMode:UIImageResizingModeStretch]];
    
 
    if ([self.model.needToSend isEqualToString:@"需要发送"]&&self.model.isSend) {
        [self SQ_SendTextMessage];
    }else{
        [self.hud stopAnimating];
    }
    if ([self.model.needToSend isEqualToString:@"发送失败"]) {
        self.reloadBtn.hidden = NO;
    } else {
        self.reloadBtn.hidden = YES;
    }
    WS(weakSelf); 
    [self.bubbleText setTextLongPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [weakSelf longAction];
    }]; 
    [self.contentView layoutIfNeeded];
}
-(void)SQ_SendTextMessage{
    self.model.needToSend = @"发送中";
   
    [self.hud startAnimating];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.model.toUser forKey:@"recvId"];
    [dic setObject:@"0" forKey:@"type"];
    [dic setObject:[RSA encryptString:self.model.content publicKey:PUBLICKey] forKey:@"content"];
    [HttpTool postJson:@"im/message/private/send" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self.hud stopAnimating];
       
        NSDictionary * datas = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        self.model.needToSend = @"发送成功";
        self.model.messageId = [NSString stringWithFormat:@"%@",[datas objectForKey:@"id"]];
        if (self.SQ_menuTouchActionBlock) {
            self.SQ_menuTouchActionBlock(@"更新会话");
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.hud stopAnimating];
        self.model.needToSend = @"发送失败";
        if (self.SQ_menuTouchActionBlock) {
            self.SQ_menuTouchActionBlock(@"更新会话");
        }
    }];
}

-(void)longAction{
    // 显示菜单栏
    SJPopMenuItem *copy = [SJPopMenuItem itemWithType:SJPopMenuItemCopy];
    SJPopMenuItem *delete = [SJPopMenuItem itemWithType:SJPopMenuItemDelete];
    NSMutableArray *items = [NSMutableArray array];
    [items addObject:copy];//复制
    [items addObject:delete];//复制
    SJPopMenu *menu = [SJPopMenu menu];
    [menu showBy:self.bubbleImageView withItems:items];
    __weak typeof(self) weakSelf = self;
    menu.itemActions = ^(SJPopMenuItemType type, NSString *title) {
        switch (type) {
            case SJPopMenuItemCopy://复制
                [weakSelf coppy];
                break;
            case SJPopMenuItemWithdraw: //撤回
                
                break;
            case SJPopMenuItemDelete: //删除
                if (self.SQ_menuTouchActionBlock) {
                    self.SQ_menuTouchActionBlock(@"删除本地");
                }
                break;
            case SJPopMenuItemSelectAll: //全选
                [weakSelf selectAllText];
                break;
            default:
                break;
        }
    };
}

- (void)coppy
{
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    pab.string = self.model.content ;
      if (pab == nil) {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制失败")];
      } else {
          [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"复制成功")];
      }
 
}

- (void)selectAllText
{
   NSLog(@"%s", __func__);
  
}



- (UIImageView *)bubbleImageView{
    if (!_bubbleImageView) {
        _bubbleImageView = [[UIImageView alloc]init];
        _bubbleImageView.userInteractionEnabled = YES;
    }
    return _bubbleImageView;
}
- (YYLabel *)bubbleText{
    if (!_bubbleText) {
        _bubbleText = [[YYLabel alloc]init];
        _bubbleText.displaysAsynchronously = YES;
        _bubbleText.ignoreCommonProperties = YES;
        _bubbleText.lineBreakMode = NSLineBreakByCharWrapping;
        _bubbleText.numberOfLines = 0;
        _bubbleText.preferredMaxLayoutWidth = SCREEN_WIDETH-118-32;
//        _bubbleText.font = FONT_REGULAR(14);
    }
    return _bubbleText;
}

 
@end
